package web;

import pojo.User;
import service.LoginService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    LoginService service = new LoginService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("执行get");
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = new User(null,username, password);
        User findUser = service.login(user);
        System.out.println(findUser);

        if(findUser==null){
            response.getWriter().println("账号不存在，请注册：");
            response.setHeader("refresh","3;regist.html");
        }else {
            response.getWriter().println("登录成功！");
        }

    }
}
